import ComponentBase from "../engine/ComponentBase";
import DateUtil from "../engine/DateUtil";
import { Logger } from "../engine/Logger";
import ResourcesManager from "../engine/ResourceManager";
import { UI_Cache_Type, UI_Config } from "../engine/UI_Config";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PokerPrefab extends ComponentBase {

    public static PREFAB_NAME: string = "PokerPrefab";

    @property({ type: cc.Node })
    private pokerCard: cc.Node = null;

    @property({ type: cc.Node })
    private pokerBack: cc.Node = null;

    @property({ type: cc.Node })
    private dizhu: cc.Node = null;

    @property({ type: cc.Node })
    private darkLayer: cc.Node = null;

    @property({ type: cc.Sprite })
    private cardSp: cc.Sprite = null

    @property({ type: cc.Node })
    private pengyouNode: cc.Node = null

    @property({ type: [cc.SpriteFrame] })
    public cardSpriteFrameList: Array<cc.SpriteFrame> = []


    private static prefab: cc.Prefab = null;

    public static instance: PokerPrefab

    //10进制对应值为
    // [
    //      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
    //      [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29], 
    //      [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45], 
    //      [49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61],
    //      [78, 79],
    //  ]

    //一副扑克牌
    //扑克定义 一个字节，前4位花色，后4位数字
    public static a_deck_cards: Array<number> = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,    //方块 A - K
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,    //梅花 A - K
        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,    //红桃 A - K
        0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,    //黑桃 A - K
        0x4e, 0x4f,                                                                      //小王,大王  
    ]

    public static Color_Type = {
        fangkuai: 0,
        meihua: 1,
        hongtao: 2,
        heitao: 3,
        wang: 4,
    }

    public prefabId: number;
    private pokerData: number = 0x01;
    public isSelect: boolean = false;
    public defaultY: number;
    private lastSelectTime: number = 0;

    public static Lai_Data: number = -1
    public static lai_check_cards: Array<number> = [
        0x4f, 0x31, 0x3D, 0x3C, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
    ]
    private static Pool_Init_Num: number = 54;
    private static pool: cc.NodePool = new cc.NodePool();


    public static toDataList(prefabList: Array<PokerPrefab>) {
        let dataList: Array<number> = [];
        for (let i = 0; i < prefabList.length; i++) {
            dataList[i] = prefabList[i].getPokerData()
        }
        return dataList;
    }

    // 获取poker的值，1，2，3，4，5，6，7，8，9，a,b,c,d
    public static getPokerValue(pokerData: number): number {
        if (isNaN(pokerData)) {
            pokerData = 0
        }
        return pokerData & 0x0f;
    }


    //获取poker花色 0,1,2,3,4
    public static getPokerColor(pokerData: number): number {
        if (isNaN(pokerData)) {
            pokerData = 0
        }
        return (pokerData & 0xF0) >> 4;
    }
    public getPokerData(): number {
        return this.pokerData;
    }

    public hideOrShowPoker(isHide: boolean) {
        this.pokerBack.active = isHide;
        if (!isHide) {
            this.pokerBack.setScale(1, 1)
        }
    }

    public showPengyou() {
        if (this.pengyouNode) {
            this.pengyouNode.active = true
        }
    }

    public showPokerAppearMv(appearTime: number = 0.2) {
        // GameMusicHelper.playOpenPoker();
        this.pokerCard.setScale(0, 1);
        this.hideOrShowPoker(true);
        let seq = cc.sequence(cc.scaleTo(appearTime, 0, 1), cc.callFunc(() => {
            // this.isAppear = true;
            this.pokerCard.runAction(cc.scaleTo(appearTime, 1, 1));
            this.hideOrShowPoker(false);
        }));
        this.pokerBack.runAction(seq);
    }

    public initPoker(pokerData: number = 0x01) {
        if (isNaN(pokerData) || pokerData == 0) {
            pokerData = 0x01;
        }
        this.dizhu.active = false;
        this.isSelect = false;
        this.darkLayer.active = false;
        this.hideOrShowPoker(true)
        this.pokerCard.setScale(1, 1);
        this.pokerBack.setScale(1, 1);
        this.pokerData = pokerData;
        let pokerColor: number = PokerPrefab.getPokerColor(this.pokerData); //花色 0,1,2,3,4
        let pokerValue: number = PokerPrefab.getPokerValue(this.pokerData);
        let index: number = pokerColor * 13 + pokerValue - 1
        if(pokerColor == PokerPrefab.Color_Type.wang){
            if(0x4e == pokerData){
                index = 52
            }else{
                index = 53
            }
        }
        this.cardSp.spriteFrame = PokerPrefab.instance.cardSpriteFrameList[index]
        this.pokerCard.active = true;
        if (this.pengyouNode) {
            this.pengyouNode.active = false
        }
    }

    public static initPool(prefab: cc.Prefab) {
        PokerPrefab.prefab = prefab;
        if(this.pool.size() <= 0){
            for (let i = 0; i < this.Pool_Init_Num; i++) {
                setTimeout(() => { //动态延迟创建
                    let tempNode: cc.Node = cc.instantiate(prefab);
                    tempNode.getComponent(PokerPrefab).prefabId = i + 1;
                    if (i == 0) {
                        PokerPrefab.instance = tempNode.getComponent(PokerPrefab)
                    } else {
                        this.pool.put(tempNode);
                    }
                }, 20 * i)
            }
        }
    }

    public setDiZhu(isDiZhu: boolean) {
        this.dizhu.active = isDiZhu
    }

    public static createOne(pokerData: number = 0x01, scale: number = 1): PokerPrefab {
        let node: cc.Node = null;
        if (this.pool.size() > 0) {
            node = this.pool.get();
        } else {
            node = cc.instantiate(this.prefab);
        }
        let poker: PokerPrefab = node.getComponent(PokerPrefab);
        poker.node.setPosition(0, 0)
        poker.defaultY = 0
        poker.unselect()
        poker.node.angle = 0
        poker.node.opacity = 255;
        poker.node.setScale(scale, scale);
        poker.isSelect = false;
        poker.initPoker(pokerData);
        return poker;
    }

    public static recoveryOne(poker: PokerPrefab) {
        if (poker) {
            if (poker.node) {
                poker.defaultY = 0
                cc.tween(poker.node).stop()
                poker.node.stopAllActions();
                poker.pokerCard.stopAllActions();
                poker.pokerBack.stopAllActions();
                // poker.node.destroy()
                this.pool.put(poker.node);
            }
        }
    }

    public static recoveryList(pokerList: Array<PokerPrefab>) {
        if (pokerList) {
            for (let i = 0; i < pokerList.length; i++) {
                if (pokerList[i]) {
                    this.recoveryOne(pokerList[i]);
                }
            }
        }
    }

    public showDark(isShow: boolean) {
        this.darkLayer.active = isShow;
    }

    public setSelect(isSelect: boolean) {
        if (this.isSelect && isSelect) {

        } else {
            this.isSelect = isSelect;
            if (this.isSelect) {
                this.defaultY = this.node.y;
                this.node.y += 50;
            } else {
                this.node.y = this.defaultY;
            }
        }
    }

    public select(selectOffectY: number = 50, isForce: boolean = false) {
        let passTime: number = DateUtil.now() - this.lastSelectTime;
        // Logger.log("poker.select", passTime, isForce)
        if (passTime <= 200 && !isForce) {
            return;
        }
        if (!this.isSelect) {
            this.defaultY = this.node.y;
            this.node.y += selectOffectY;
            this.isSelect = true
        } else {
            this.node.y = this.defaultY;
            this.isSelect = false;
        }
        this.lastSelectTime = DateUtil.now()
    }

    public unselect() {
        if (isNaN(this.defaultY)) {
            this.defaultY = this.node.y;
        }
        this.node.y = this.defaultY;
        this.isSelect = false;
        this.lastSelectTime = 0
    }


}
